/*
 * HomeHotGoodsListItem.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.widget.dialog.QuickBuyDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.HomeGoodsHolder;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

/**
 * 首页热门商品的ListItem
 */
public class HomeGoodsListItem extends DisplayListItem<HomeGoodsHolder, List<Goods>>
        implements View.OnClickListener {

    private Activity mActivity;

    private Goods mGoods1;
    private Goods mGoods2;

    public HomeGoodsListItem(List<Goods> goodsList) {
        super(goodsList);
    }

    @Override
    public void onShow(Context context, HomeGoodsHolder holder) {
        mActivity = (Activity) context;

        try {
            // 第一个商品
            mGoods1 = data.get(0);
            holder.refreshUI1(mGoods1);

            // 事件
            holder.mIvGoodsImage1.setOnClickListener(this);
            holder.mTvGoodsPrice1.setOnClickListener(this);
            holder.mIvShoppingCart1.setOnClickListener(this);

            // 第二个商品
            mGoods2 = data.get(1);
            holder.mLlytGoods2.setVisibility(View.VISIBLE);
            holder.refreshUI2(mGoods2);

            // 事件
            holder.mIvGoodsImage2.setOnClickListener(this);
            holder.mTvGoodsPrice2.setOnClickListener(this);
            holder.mIvShoppingCart2.setOnClickListener(this);
        } catch (IndexOutOfBoundsException e) {
            if (mGoods2 == null) {
                holder.mLlytGoods2.setVisibility(View.INVISIBLE);
            }
        } finally {
            holder.showRootLayoutDivider(mGoods2 == null ? mGoods1.isLast() : mGoods2.isLast());
        }
    }

    @Override
    public String getDisplayItemTag() {
        return HomeGoodsListItem.class.getSimpleName();
    }

    @Override
    public Class<HomeGoodsHolder> getViewHolderClazz() {
        return HomeGoodsHolder.class;
    }

    @Override
    public void onClick(View v) {

        Goods goods1 = null;
        Goods goods2 = null;

        try {
            goods1 = data.get(0);
            goods2 = data.get(1);
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }

        switch (v.getId()) {
            case R.id.iv_goods_image_1:
                UmengUtils.onEvent(mActivity, R.string.umeng_home_to_goods_detail);
                GoodsDetailActivity.actionStart(mActivity, goods1.getGoodsId());
                break;

            case R.id.iv_goods_image_2:
                UmengUtils.onEvent(mActivity, R.string.umeng_home_to_goods_detail);
                GoodsDetailActivity.actionStart(mActivity, goods2.getGoodsId());
                break;

//            case R.id.tv_goods_price_1:
//            case R.id.tv_goods_price_2:
//                if (!PriceUtils.isLogin()) {
//                    LoginActivity.actionStart(mActivity);
//                }
//                break;

            // 加入购物车/立即购买
            case R.id.iv_shopping_cart_1:
                // 未登录 --> 去登录
//                if (!PriceUtils.isLogin()) {
//                    LoginActivity.actionStart(mActivity);
//                    return;
//                }

                // 已登录，获取商品购物车信息
                getGoodsCartInfo(goods1);
                break;
            case R.id.iv_shopping_cart_2:
                // 未登录 --> 去登录
//                if (!PriceUtils.isLogin()) {
//                    LoginActivity.actionStart(mActivity);
//                    return;
//                }

                // 已登录，获取商品购物车信息
                getGoodsCartInfo(goods2);
                break;
        }
    }

    private void getGoodsCartInfo(final Goods goods) {

        // 获取商品的购物车信息
        float goodsPrice = Float.parseFloat(goods.getShopPriceFloat());
        int goodsNumber = 1;
        int stockNumber = goods.getGoodsNumber();
        String goodsId = goods.getGoodsId();
        int minNumber = goods.getMinNumber();

        // 最少起订量大于库存,无法购买
        if (minNumber > stockNumber) {
            UIUtils.showToast(UIUtils.getString(R.string.can_not_buy_when_out_of_stock));
            return;
        }

        Goods newGoods = new Goods();
        newGoods.setGoodsId(goodsId);
        newGoods.setStockNumber(stockNumber);
        newGoods.setGoodsPrice(goodsPrice);
        newGoods.setGoodsNumber(goodsNumber);
        newGoods.setGoodsName(goods.getGoodsName());
        newGoods.setFavourableNotice(goods.getFavourableNotice());
        newGoods.setMinNumber(goods.getMinNumber());
        newGoods.setLimitNumber(goods.getLimitNumber());
        showAddToCartDialog(newGoods);
    }

    private void showAddToCartDialog(Goods goods) {

        QuickBuyDialog dialog = new QuickBuyDialog(mActivity);
        dialog.show();

        dialog.setGoods(goods);
    }
}
